

#include <gtk/gtk.h>
#include "support.h"
#include "window.h"
#include "sourceview.h"
#include "statusbar.h"
#include "widget.h"

/*################# Static Functions ####################*/
static void sourceview_docked(void);

/*############### Globals ######################*/
GtkWidget *sourceview;
GtkTextBuffer *sourceview_buffer;
GtkWidget *dock_window;
gint docked;

/*###################### Show / hide Sourceview ######################*/
gboolean show_hide_sourceview(void)
{
	if(state_sourceview)
	{
		state_sourceview=0;
		if(docked)
			gtk_widget_hide_all(dock_window);
		else
			gtk_widget_hide_all(gsim_sourceview);
		gsim_update_statusbar(DESC_SBAR,_("Sourceview off"));
	}
	else
	{
		state_sourceview=1;
		if(docked)
			gtk_widget_show_all(dock_window);
		else
			gtk_widget_show_all(gsim_sourceview);
		gsim_update_statusbar(DESC_SBAR,_("Sourceview on"));
	}
	return TRUE;
}

/*############### Sourceview docked/undocked #########################*/
static void sourceview_docked(void)
{
	if(docked==0)
	{
		gtk_widget_reparent(gsim_sourceview,dock_window);
		gtk_widget_show_all(dock_window);
		docked=1;
		gsim_update_statusbar(DESC_SBAR,_("Sourceview Docked"));
	}
	else
	{
		gtk_widget_reparent(gsim_sourceview,gsim_sourceview_box);
		gtk_widget_hide(dock_window);
		docked=0;
		gsim_update_statusbar(DESC_SBAR,_("Sourceview Unocked"));
	}
}

/*################ reste the source view #################*/
void gsim_reset_sourceview(void)
{
	gtk_text_buffer_set_text(sourceview_buffer,"",0);
}

/*############### scroll to the end of sourceview ###########*/
void sourceview_scroll_end(void)
{
	GtkTextIter iter;
	gtk_text_buffer_get_end_iter(sourceview_buffer,&iter);
	gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(sourceview),&iter,0,0,0,0);
}

/*########## Make the view where source is back generated ############*/
GtkWidget *gsim_get_sourceview_box( void )
{
	GtkWidget *sourceview_table;
	sourceview_table=gtk_table_new(7,2,FALSE);
	
	/*pack the various buttons*/
	GtkWidget *sourceview_close;
	sourceview_close=gsim_small_close_button_new(-1,-1);;
//	gtk_button_set_relief(GTK_BUTTON(sourceview_close), GTK_RELIEF_HALF);
	//gtk_widget_set_size_request( sourceview_close , 20, 20 );
	gtk_table_attach( GTK_TABLE( sourceview_table ), sourceview_close, 0 , 1 , 0 , 1 , 0 , 0, 0 , 1 );
	gtk_tooltips_set_tip( gsim_tooltips, sourceview_close, _("Close the source window "),NULL);
	g_signal_connect(G_OBJECT(sourceview_close),"clicked",G_CALLBACK(show_hide_sourceview),NULL);
	
	GtkWidget *sourceview_dock;//,*dock_label;
	sourceview_dock=gsim_small_dock_button_new(-1,-1);
	/*dock_label=gtk_label_new("<span size=\"small\">D</span>");
	gtk_label_set_use_markup(GTK_LABEL(dock_label),TRUE);
	gtk_container_add(GTK_CONTAINER(sourceview_dock),dock_label);
	gtk_button_set_relief(GTK_BUTTON(sourceview_dock), GTK_RELIEF_HALF);
	gtk_widget_set_size_request( sourceview_dock , 20, 20 );*/
	gtk_table_attach( GTK_TABLE( sourceview_table ), sourceview_dock, 0 , 1 , 1 , 2 , 0 , 0, 0 , 1 );
	gtk_tooltips_set_tip( gsim_tooltips, sourceview_dock, _("Dock/Undock the source window "),NULL);
	g_signal_connect(sourceview_dock,"clicked",G_CALLBACK(sourceview_docked),NULL);
	
	GtkWidget *sourceview_save;
	sourceview_save=gsim_empty_button_new(-1,-1);
	//gtk_button_set_relief(GTK_BUTTON(sourceview_save), GTK_RELIEF_HALF);
	//gtk_widget_set_size_request( sourceview_save , 22, 22 );
	gtk_table_attach( GTK_TABLE( sourceview_table ), sourceview_save,0 , 1 , 3 , 4 , 0 , 0, 0 , 1 );
	//gtk_tooltips_set_tip( gsim_tooltips, sourceview_save, "Save ",NULL);
	
	GtkWidget *sourceview_reset;//,*reset_label;
	sourceview_reset=gsim_small_reset_button_new(-1,-1);
	/*reset_label=gtk_label_new("<span size=\"small\">R</span>");
	gtk_label_set_use_markup(GTK_LABEL(reset_label),TRUE);
	gtk_container_add(GTK_CONTAINER(sourceview_reset),reset_label);
	gtk_button_set_relief(GTK_BUTTON(sourceview_reset), GTK_RELIEF_HALF);
	gtk_widget_set_size_request( sourceview_reset , 20, 20 );*/
	gtk_table_attach( GTK_TABLE( sourceview_table ), sourceview_reset, 0 , 1 , 2 , 3 , 0 , 0, 0 , 1 );
	gtk_tooltips_set_tip( gsim_tooltips, sourceview_reset, _("Reset "),NULL);
	g_signal_connect(G_OBJECT(sourceview_reset),"clicked",G_CALLBACK(gsim_reset_sourceview),NULL);
	
	GtkWidget *sourceview_print;
	sourceview_print=gsim_empty_button_new(-1,-1);
	//gtk_button_set_relief(GTK_BUTTON(sourceview_print), GTK_RELIEF_HALF);
	//gtk_widget_set_size_request( sourceview_print , 22, 22 );
	gtk_table_attach( GTK_TABLE( sourceview_table ), sourceview_print, 0 , 1 , 4 , 5 , 0 , 0, 0 , 1 );
	//gtk_tooltips_set_tip( gsim_tooltips, sourceview_print, "Print",NULL);
	
	GtkWidget *sourceview_config;
	sourceview_config=gsim_empty_button_new(-1,-1);
	//gtk_button_set_relief(GTK_BUTTON(sourceview_config), GTK_RELIEF_HALF);
	//gtk_widget_set_size_request( sourceview_config , 22, 22 );
	gtk_table_attach( GTK_TABLE( sourceview_table ), sourceview_config, 0 , 1 , 5 , 6 , 0 , 0, 0 , 1 );
	//gtk_tooltips_set_tip( gsim_tooltips, sourceview_config, "Preferences",NULL);
	
	/*TextView where all source will appear*/
  	sourceview = gtk_text_view_new ();
  	sourceview_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (sourceview));
	gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(sourceview),FALSE);
	
	/*pack view in scroll window*/
	GtkWidget *sourceview_scroll;
	sourceview_scroll=gtk_scrolled_window_new(NULL,NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sourceview_scroll),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sourceview_scroll),GTK_SHADOW_IN);
	gtk_table_attach( GTK_TABLE( sourceview_table ), sourceview_scroll, 1 , 2 , 0 , 7 , 
											GTK_FILL|GTK_EXPAND , GTK_FILL|GTK_EXPAND, 0 , 0 );
	gtk_container_add(GTK_CONTAINER(sourceview_scroll),sourceview);
	
	/*create the window for docking*/
	dock_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_object_set( dock_window ,"window-position",GTK_WIN_POS_CENTER,
																"default-width",700,
																"title",_("Sourceview"),NULL);
	g_signal_connect(dock_window,"delete-event",G_CALLBACK(show_hide_sourceview),NULL);
	
	/*pack everything in the frame*/
	GtkWidget *sourceview_frame;
	sourceview_frame=gtk_frame_new(NULL);
	gtk_container_add(GTK_CONTAINER(sourceview_frame),sourceview_table);
	
	return sourceview_frame;
}
